package com.lsfb.duishangbaos.Order;

import android.content.Intent;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lsfb.duishangbaos.MoneyManage.MyDetailBean;
import com.lsfb.duishangbaos.MoneyManage.MyDetailEvent;
import com.lsfb.duishangbaos.R;
import com.lsfb.duishangbaos.Utils.LittleUtils;
import com.lsfb.duishangbaos.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.LsfbLog;
import com.zgscwjm.lsfbbasetemplate.utils.SN;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/7/24 0024.
 */
@ContentView(R.layout.aty_order_manage)
public class OrderManageActivity extends LsfbActivity {

    @ViewInject(R.id.aty_listview)
    private PullToRefreshListView listview;

    private List<MyDetailBean.ListBean> mList;

    private EarningsMoneyAdapter adapter;

    private boolean IsRefresh = false;

    private int page = 0;

    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "订单管理");

        mList = new ArrayList<>();
        adapter = new EarningsMoneyAdapter(this, R.layout.item_money, mList);
        listview.setAdapter(adapter);

        listview.setMode(PullToRefreshBase.Mode.BOTH);
        listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 0;
                IsRefresh = true;
                getData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                IsRefresh = false;
                page++;
                getData();
            }
        });
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {
        getData();
    }

    public void getData() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("cla", "2");//1 提现 ，2 收益
        hashMap.put("num", "15");
        hashMap.put("page", String.valueOf(page));
        new BaseInternet().getData_Get(URLString.MYDETAIL, hashMap, MyDetailBean.class, new MyDetailEvent<MyDetailBean>(), true,true);
    }

    @EventAnnotation
    public void MyDetailEvent(MyDetailEvent<MyDetailBean> event) {
        switch (event.getCode()) {
            case 200:
                if (IsRefresh) {
                    mList.clear();
                }
                mList.addAll(event.getData().getList());
                adapter.notifyDataSetChanged();
                break;
            case 1002:
                if (IsRefresh) {
                    mList.clear();
                } else {
                    page--;
                    LsfbLog.e("上拉订单没更多数据" + event.getMsg());
                }
                SN.SnackShow(listview, "暂无记录");
                adapter.notifyDataSetChanged();
                break;
            default:
                mList.clear();
                page = 0;
                adapter.notifyDataSetChanged();
                SN.SnackShow(listview, event.getMsg());
                break;
        }
    }

    /**查看订单详情
     * @param event
     */
    @EventAnnotation
    public void OrderClickEvent(OrderClickEvent event){
          startActivity(new Intent(this,OrderDetailsActivity.class).putExtra("id",event.getData()));
    }


}
